package cj.web.admin.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.web.admin.dao.SurveyDAO;
import cj.web.admin.dao.SurveyDepotTypeDAO;
import cj.web.admin.domain.Survey;
import cj.web.admin.domain.SurveyDepotType;
import cj.web.constants.MemuEnum;
import cj.web.login.controllers.BaseController;

@Path("/survey")
public class SurveyController extends BaseController{

	private static Logger logger = Logger.getLogger(SurveyController.class.getName());
	
	@Autowired
	private SurveyDAO surveyDao;
	@Autowired
	private SurveyDepotTypeDAO surveyDepotTypeDao;
	
	@Get("show")
	public String show(Model model,Survey survey) {
	
		List<SurveyDepotType> objlis = surveyDepotTypeDao.queryAll();
		//MemuEnum
        model.add("MemuEnum",MemuEnum.lkgkssj);
        model.add("surveyDepotTypelis",objlis);
		return "/views/survey/surveyList.vm";
	}

	@Post("getList")
	public String queryList(Survey survey) {
		JSONObject jo = new JSONObject();
		List<Survey> arr = surveyDao.query(survey);
		for(Survey obj:arr){
			obj.setVoteStart(DateUtils.getMinusTime(obj.getVoteStart(), DateUtils.DATE_FULL_STR, 0));
			obj.setVoteEnd(DateUtils.getMinusTime(obj.getVoteEnd(), DateUtils.DATE_FULL_STR, 0));
		}
		int size = surveyDao.querysize(survey);
		jo.put("data", arr);
		jo.put("draw", survey.getDraw());
		jo.put("recordsTotal", size);
		jo.put("recordsFiltered", size);
		return "@" + jo.toString();
	}
	
	
	@Get("createPage")
	public String createPage(Model model){
        List<SurveyDepotType> objlis = surveyDepotTypeDao.queryAll();
        //MemuEnum
        model.add("MemuEnum",MemuEnum.lkgkssj);
        model.add("surveyDepotTypelis",objlis);
		return "/views/survey/surveyDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,Survey survey,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			survey.setCreateUser(returnSessionObj(request).getAccount());
			survey.setUpdateUser(returnSessionObj(request).getAccount());
			surveyDao.create(survey);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id){
		
		Survey survey =surveyDao.queryById(id);
		
		List<SurveyDepotType> objlis = surveyDepotTypeDao.queryAll();
        //MemuEnum
        model.add("MemuEnum",MemuEnum.lkgkssj);
        model.add("surveyDepotTypelis",objlis);
        model.add("obj",survey);
		return "/views/survey/surveyEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,Survey survey,HttpServletRequest request){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			survey.setUpdateUser(returnSessionObj(request).getAccount());
			surveyDao.update(survey);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("id") int id){
		surveyDao.deleteById(id);
		return "r:show";
	}
}